package com.grass.p019lv.novel.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.androidsa.bttsddka.R;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.androidx.p013lv.base.view.StatusControlLayout;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.grass.appointment.comment.InputTextDialog;
import com.grass.p019lv.bean.NovelCommentBean;
import com.grass.p019lv.bean.NovelCommentData;
import com.grass.p019lv.bean.NovelReplyBean;
import com.grass.p019lv.novel.adapter.NovelCommentAdapter;
import com.grass.p019lv.novel.fragment.NovelCommentVerticalLayout;
import com.grass.p019lv.novel.viewmodel.NovelCommentModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/* renamed from: com.grass.lv.novel.fragment.NovelCommentFragment */
public class NovelCommentFragment extends BottomSheetDialogFragment implements OnLoadMoreListener, ItemClickListener, NovelCommentVerticalLayout.OnTwoClickListener {
    private NovelCommentAdapter adapter;
    private int chapterId;
    private NovelCommentInterface commentInterface;
    private int commentTotal;
    private int fictionId;
    private InputTextDialog inputTextDialog;
    private String lastId;
    private NovelCommentModel model;
    private int position;
    private SmartRefreshLayout refresh;
    private StatusControlLayout status_layout;
    private int total;
    private TextView tv_total;
    private int userId;
    private View view;

    /* renamed from: com.grass.lv.novel.fragment.NovelCommentFragment$NovelCommentInterface */
    public interface NovelCommentInterface {
        void addCommentNum(int i, int i2);
    }

    public static NovelCommentFragment newInstance() {
        Bundle bundle = new Bundle();
        NovelCommentFragment novelCommentFragment = new NovelCommentFragment();
        novelCommentFragment.setArguments(bundle);
        return novelCommentFragment;
    }

    public void setCommentInterface(NovelCommentInterface novelCommentInterface) {
        this.commentInterface = novelCommentInterface;
    }

    @Override // androidx.appcompat.app.AppCompatDialogFragment, com.google.android.material.bottomsheet.BottomSheetDialogFragment, androidx.fragment.app.DialogFragment
    public Dialog onCreateDialog(Bundle bundle) {
        BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) super.onCreateDialog(bundle);
        View inflate = View.inflate(getContext(), R.layout.comment_layout, null);
        this.view = inflate;
        bottomSheetDialog.setContentView(inflate);
        ((View) this.view.getParent()).setBackgroundColor(getResources().getColor(17170445));
        bottomSheetDialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(17170445);
        BottomSheetBehavior.from((View) this.view.getParent()).setPeekHeight(UiUtils.dp2px(531));
        return bottomSheetDialog;
    }

    @Override // androidx.fragment.app.Fragment, androidx.fragment.app.DialogFragment
    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        initView(this.view);
        this.status_layout.showLoading();
        this.lastId = "";
        this.model.loadNovelComment(this.fictionId, this.chapterId, "");
    }

    private void initView(View view2) {
        this.model = (NovelCommentModel) new ViewModelProvider(this).get(NovelCommentModel.class);
        NovelCommentAdapter novelCommentAdapter = new NovelCommentAdapter();
        this.adapter = novelCommentAdapter;
        novelCommentAdapter.setOnTwoClickListener(this);
        this.adapter.setImgDomain(SpUtils.getInstance().getString("domain"));
        this.adapter.setUserId(this.userId);
        this.status_layout = (StatusControlLayout) view2.findViewById(R.id.status_layout);
        SmartRefreshLayout smartRefreshLayout = (SmartRefreshLayout) view2.findViewById(R.id.refresh);
        this.refresh = smartRefreshLayout;
        smartRefreshLayout.setEnableRefresh(false);
        RecyclerView recyclerView = (RecyclerView) view2.findViewById(R.id.recycler);
        this.refresh.setOnLoadMoreListener(this);
        this.adapter.setItemListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(this.adapter);
        TextView textView = (TextView) view2.findViewById(R.id.tv_total);
        this.tv_total = textView;
        textView.setText(this.commentTotal + "條評論");
        this.model.commentList().observe(this, new Observer<BaseRes<NovelCommentData>>() {
            /* class com.grass.p019lv.novel.fragment.NovelCommentFragment.C31891 */

            public void onChanged(BaseRes<NovelCommentData> baseRes) {
                NovelCommentFragment.this.hideLoading();
                if (baseRes.getCode() == 200) {
                    NovelCommentData data = baseRes.getData();
                    NovelCommentFragment.this.total = data.getTotal();
                    List<NovelCommentBean> data2 = data.getData();
                    if (TextUtils.isEmpty(NovelCommentFragment.this.lastId)) {
                        NovelCommentFragment.this.adapter.setData(data2);
                    } else {
                        NovelCommentFragment.this.adapter.setDatasInEnd(data2);
                    }
                    if (data2.size() >= 1) {
                        NovelCommentFragment.this.lastId = data2.get(data2.size() - 1).getId();
                    }
                    if (NovelCommentFragment.this.total != 0 && NovelCommentFragment.this.total == NovelCommentFragment.this.adapter.getItemCount()) {
                        NovelCommentFragment.this.refresh.finishLoadMore(0, true, true);
                        return;
                    }
                    return;
                }
                NovelCommentFragment.this.status_layout.showError();
            }
        });
        view2.findViewById(R.id.ll_comment).setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.novel.fragment.NovelCommentFragment.View$OnClickListenerC31902 */

            public void onClick(View view) {
                if (SpUtils.getInstance().getUserInfo().getFreeWatches() != -1) {
                    ToastUtils.getInstance().showSigh("VIP用戶才能參與評論哦");
                } else {
                    NovelCommentFragment.this.initInputTextMsgDialog(null);
                }
            }
        });
        view2.findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.novel.fragment.NovelCommentFragment.View$OnClickListenerC31913 */

            public void onClick(View view) {
                NovelCommentFragment.this.dismiss();
            }
        });
        this.model.addComment().observe(this, new Observer<BaseRes<NovelCommentBean>>() {
            /* class com.grass.p019lv.novel.fragment.NovelCommentFragment.C31924 */

            public void onChanged(BaseRes<NovelCommentBean> baseRes) {
                if (baseRes.getCode() != 200 || baseRes.getData() == null) {
                    ToastUtils.getInstance().showWeak(baseRes.getMsg());
                    return;
                }
                NovelCommentFragment.this.addCommentBean(baseRes.getData());
                ToastUtils.getInstance().showCorrect("評論成功");
            }
        });
        this.model.addCommentReply().observe(this, new Observer<BaseRes<NovelReplyBean>>() {
            /* class com.grass.p019lv.novel.fragment.NovelCommentFragment.C31935 */

            public void onChanged(BaseRes<NovelReplyBean> baseRes) {
                if (baseRes.getCode() != 200 || baseRes.getData() == null) {
                    ToastUtils.getInstance().showWeak(baseRes.getMsg());
                    return;
                }
                NovelCommentFragment.this.addCommentReplyBean(baseRes.getData());
                ToastUtils.getInstance().showCorrect("評論成功");
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void initInputTextMsgDialog(final NovelCommentBean novelCommentBean) {
        dismissInputDialog();
        if (this.inputTextDialog == null) {
            InputTextDialog inputTextDialog2 = new InputTextDialog(getContext(), R.style.dialogCenter);
            this.inputTextDialog = inputTextDialog2;
            inputTextDialog2.setOnTextSendListener(new InputTextDialog.OnTextSendListener() {
                /* class com.grass.p019lv.novel.fragment.NovelCommentFragment.C31946 */

                @Override // com.grass.appointment.comment.InputTextDialog.OnTextSendListener
                public void onTextSend(String str) {
                    if (novelCommentBean == null) {
                        NovelCommentFragment.this.model.commitComment(NovelCommentFragment.this.fictionId, NovelCommentFragment.this.chapterId, str);
                    } else {
                        NovelCommentFragment.this.model.replyOneComment(novelCommentBean, str);
                    }
                }
            });
        }
        this.inputTextDialog.show();
    }

    private void dismissInputDialog() {
        InputTextDialog inputTextDialog2 = this.inputTextDialog;
        if (inputTextDialog2 != null) {
            if (inputTextDialog2.isShowing()) {
                this.inputTextDialog.dismiss();
            }
            this.inputTextDialog.cancel();
            this.inputTextDialog = null;
        }
    }

    public void hideLoading() {
        this.status_layout.hideLoading();
        this.refresh.finishLoadMore();
    }

    public void loadNovelComment(int i, int i2, int i3, int i4, int i5) {
        this.userId = i;
        this.fictionId = i2;
        this.chapterId = i3;
        this.commentTotal = i4;
        this.position = i5;
    }

    @Override // com.androidx.p013lv.base.listener.ItemClickListener
    public void onItemClick(View view2, int i) {
        if (SpUtils.getInstance().getUserInfo().getFreeWatches() != -1) {
            ToastUtils.getInstance().showSigh("VIP用戶才能參與評論哦");
        } else {
            initInputTextMsgDialog((NovelCommentBean) this.adapter.getDataInPosition(i));
        }
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        this.model.loadNovelComment(this.fictionId, this.chapterId, this.lastId);
    }

    @Override // androidx.fragment.app.DialogFragment
    public void show(FragmentManager fragmentManager, String str) {
        try {
            Class<?> cls = Class.forName("androidx.fragment.app.DialogFragment");
            Field declaredField = cls.getDeclaredField("mDismissed");
            declaredField.setAccessible(true);
            declaredField.setBoolean(this, false);
            Field declaredField2 = cls.getDeclaredField("mShownByMe");
            declaredField2.setAccessible(true);
            declaredField2.setBoolean(this, true);
            FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
            beginTransaction.add(this, str);
            beginTransaction.commitAllowingStateLoss();
        } catch (Exception unused) {
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void addCommentBean(NovelCommentBean novelCommentBean) {
        this.status_layout.hideLoading();
        this.adapter.setDataInStart(novelCommentBean);
        this.commentTotal++;
        this.tv_total.setText(this.commentTotal + "條評論");
        NovelCommentInterface novelCommentInterface = this.commentInterface;
        if (novelCommentInterface != null) {
            novelCommentInterface.addCommentNum(this.position, this.commentTotal);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void addCommentReplyBean(NovelReplyBean novelReplyBean) {
        List data = this.adapter.getData();
        for (int i = 0; i < data.size(); i++) {
            NovelCommentBean novelCommentBean = (NovelCommentBean) data.get(i);
            if (novelReplyBean.getParentId().equals(novelCommentBean.getId())) {
                List<NovelReplyBean> reply = novelCommentBean.getReply();
                if (reply == null) {
                    reply = new ArrayList<>();
                }
                reply.add(0, novelReplyBean);
                novelCommentBean.setReply(reply);
                this.adapter.setItemRefresh(true);
                this.adapter.notifyItemChanged(i, Integer.valueOf((int) R.id.layout_comment2));
            }
        }
        this.commentTotal++;
        this.tv_total.setText(this.commentTotal + "條評論");
        NovelCommentInterface novelCommentInterface = this.commentInterface;
        if (novelCommentInterface != null) {
            novelCommentInterface.addCommentNum(this.position, this.commentTotal);
        }
    }

    @Override // com.grass.p019lv.novel.fragment.NovelCommentVerticalLayout.OnTwoClickListener
    public void onTwoClick(NovelReplyBean novelReplyBean, String str) {
        if (SpUtils.getInstance().getUserInfo().getFreeWatches() != -1) {
            ToastUtils.getInstance().showSigh("VIP用戶才能參與評論哦");
        } else {
            initInputTextMsgTwoDialog(novelReplyBean, str);
        }
    }

    private void initInputTextMsgTwoDialog(final NovelReplyBean novelReplyBean, String str) {
        dismissInputDialog();
        if (this.inputTextDialog == null) {
            InputTextDialog inputTextDialog2 = new InputTextDialog(getContext(), R.style.dialogCenter);
            this.inputTextDialog = inputTextDialog2;
            inputTextDialog2.setOnTextSendListener(new InputTextDialog.OnTextSendListener() {
                /* class com.grass.p019lv.novel.fragment.NovelCommentFragment.C31957 */

                @Override // com.grass.appointment.comment.InputTextDialog.OnTextSendListener
                public void onTextSend(String str) {
                    NovelCommentFragment.this.model.replyTwoComment(novelReplyBean, str);
                }
            });
        }
        this.inputTextDialog.show();
    }

    @Override // com.google.android.material.bottomsheet.BottomSheetDialogFragment, androidx.fragment.app.DialogFragment
    public void dismiss() {
        dismissAllowingStateLoss();
    }
}
